package com.work.dyduser.module.pagemine;

import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.work.dyduser.R;
import com.work.dyduser.base.BaseActivity;
import com.work.dyduser.bean.PhoneCheck;
import com.work.dyduser.bean.WeixinData;
import com.work.dyduser.httpretrofit.MyCallback;
import com.work.dyduser.httpretrofit.PostBody;
import com.work.dyduser.httpretrofit.RetrofitManager;
import com.work.dyduser.utils.CountdownButton;
import com.work.dyduser.utils.JsonUtils;
import com.work.dyduser.utils.UIUtils;
import com.work.dyduser.utils.VerificationUtil;
import com.work.dyduser.utils.ViewUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;

/**
 * author: Aly
 * date :2018/12/7 13:32
 * version:1.0
 * description:绑定手机号
 */
public class BindPhoneActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_phonecode)
    EditText etPhonecode;
    @BindView(R.id.cbt_time)
    CountdownButton cbtTime;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.lt_help)
    LinearLayout ltHelp;
    private String data;
    private WeixinData weixinData;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pagemine_forgetpassword;
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("绑定手机号");
        ltHelp.setVisibility(View.GONE);
        ViewUtils.setOnClickListeners(this, cbtTime, tvNext, tvTel);
    }

    @Override
    protected void initData() {
        data = getIntent().getStringExtra("data");
        weixinData = JsonUtils.parseJsonToBean(data, WeixinData.class);
    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.cbt_time:
                //获取短信验证码
                if (VerificationUtil.isPhone(etPhone.getText().toString())) {
                    getHttpCode(etPhone.getText().toString());
                    cbtTime.start();
                }
                break;
            case R.id.tv_next:
                //下一步
                getHttpchecksmscode();

//                UIUtils.startActivity(ResetPasswordActivity.class);
                break;
        }
    }

    //获取验证码
    private void getHttpCode(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        Call<Object> call = RetrofitManager.getInstance().getsmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<Object>() {
            @Override
            public void onSuccess(Object response) {
                UIUtils.showToast("验证码发送成功");
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    //校验验证码
    private void getHttpchecksmscode() {
        showLoaddialog();
        final String phone = etPhone.getText().toString();
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        map.put("smscode", etPhonecode.getText().toString());
        Call<PhoneCheck> call = RetrofitManager.getInstance().checksmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<PhoneCheck>() {
            @Override
            public void onSuccess(PhoneCheck response) {
                if (response.getErrcode() == 0) {
                    //验证码校验成功
//                    wechatblind();
                } else {
                    UIUtils.showToast("验证码验证失败");
                    hideLoaddialog();
                }

            }


            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast("errMsg");
                hideLoaddialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

//    //绑定微信
//    private void wechatblind() {
//        showLoaddialog();
//        Map<String, String> map = new HashMap<>();
//        map.put("phone_type", "Android");
//        map.put("open_id", weixinData.getOpenid());
//        map.put("user_picture", weixinData.getIcon());
//        map.put("nickname", weixinData.getNickname());
//        map.put("bindingPhone", etPhone.getText().toString());
//        Call<UserMessage> call = RetrofitManager.getInstance().wechatblind(PostBody.toBody(map));
//        call.enqueue(new MyCallback<UserMessage>() {
//            @Override
//            public void onSuccess(UserMessage response) {
//                if (response.getErrcode() == 0) {
//                    SPutils.put(Ckey.USERMESSAGE, JsonUtils.parseBeantojson(response));
//                    SPutils.put(Ckey.USERID, response.getData().getUser_id());
//                    UIUtils.startActivity(MainActivity.class);
//                    finish();
//                } else {
//                    UIUtils.showToast(response.getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(int errorCode, String errMsg) {
//
//            }
//
//            @Override
//            public void onComplete() {
//                hideLoaddialog();
//            }
//        });
//
//    }
}
